package com.lin.security;


import com.lin.entity.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author linze
 * @version 1.0
 * @Description
 * @Date 2021/1/21 18:03
 */
public class SecurityUser implements UserDetails {

    private long uid;

    public long getUid() {
        return uid;
    }

    public void setUid(long uid) {
        this.uid = uid;
    }

    private String userName;
    private String passWord;
    private String role;

    public String getRole() {
        return role;
    }

    List<GrantedAuthority> authorities;
    public SecurityUser(String username,String role){
        authorities = new ArrayList<>();
        this.role=role;
        authorities.add(new SimpleGrantedAuthority(role));
    }
    public SecurityUser(long uid,String username,String role){
        authorities = new ArrayList<>();
        this.role=role;
        authorities.add(new SimpleGrantedAuthority(role));
    }
    public SecurityUser(User user) {
        this.uid=user.getUid();
        this.userName=user.getUsername();
        this.passWord=user.getPassword();
        this.role=String.valueOf(user.getIdentity());
         authorities = new ArrayList<>();
         authorities.add(new SimpleGrantedAuthority(String.valueOf(user.getIdentity())));
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return passWord;
    }

    @Override
    public String getUsername() {
        return userName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public String toString() {
        return "SecurityUser{" +
                "userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                ", role='" + role + '\'' +
                ", authorities=" + authorities +
                '}';
    }
}
